<?php
/**
 * Created by PhpStorm.
 * User: Keter
 * Date: 2016/7/28
 * Time: 20:26
 */
/**
 *  自动注册
 */
class DatabaseBase extends Base
{
    public static $mysqlConnect;
    public static $redisConnect;

    public function __construct()
    {
        $this->initRedis();
    }

    /**
     * 初始化当前Redis
     */
    public function initRedis()
    {
        if (!is_object(self::$redisConnect['default'])) {
            self::$redisConnect['default'] = $this->connectRedis();
        }

        $this->redis = self::$redisConnect['default'];
    }

    /**
     * 关闭Redis连接
     */
    public function deinitRedis($serverName = NULL)
    {
        if(0 < strlen($serverName))
        {
            if(isset(self::$redisConnect[$serverName]))
            {
                self::$redisConnect[$serverName]->close();
                unset(self::$redisConnect[$serverName]);
            }
        }
        else
        {
            $this->redis->close();
        }
    }

    /**
     * 初始化当前mysql
     */
    public function initMysql()
    {
        /**
         * mysqli类没有长连接，暂时先用短连接
         */
        $this->mysql = $this->connectMysql();

        /**
         * TODO:留用
         */
        self::$mysqlConnect['default'] = $this->mysql;
    }

    /**
     * 关闭当前Mysql连接
     */
    public function deinitMysql($serverName = NULL)
    {
        if(0 < strlen($serverName))
        {
            if(isset(self::$mysqlConnect[$serverName]))
            {
                self::$mysqlConnect[$serverName]->close();
                unset(self::$mysqlConnect[$serverName]);
            }
        }
        else
        {
            $this->mysql->close();
        }
    }

    /**
     * 调用Redis
     * $dbName如为空，则选择默认Redis库
     * @param null $serverName
     * @param null $dbName
     * @return mixed
     */
    public function redis($serverName = null, $dbName = null)
    {
        if (empty($serverName)) {
            $serverName = 'default';
        }

        if (isset(self::$redisConnect[$serverName])) {
            $redis = self::$redisConnect[$serverName];
            if ($dbName) {
                $redis->select($dbName);
            }
        } else {
            self::$redisConnect[$serverName] = $this->connectRedis($serverName);
            $redis = self::$redisConnect[$serverName];
        }

        if ($dbName && is_integer($dbName)) {
            $redis->select($dbName);
        }

        return $redis;
    }

    /**
     * 调用数据库
     * $dbName如为空，则选择默认Mysql库
     * @param null $serverName
     * @param null $dbName
     */
    public function mysql($serverName = null, $dbName = null)
    {
        if (empty($serverName)) {
            $serverName = 'default';
        }

        // 连接断开自动重连
        if(!($mysql = self::$mysqlConnect[$serverName]) || !$mysql->ping())
        {
            $mysql && !$mysql->ping() && $this->i('mysql_has_gone_away_and_reconnect');
            $this->deinitMysql($serverName);
            $mysql = $this->connectMysql($serverName, $dbName);
            self::$mysqlConnect[$serverName] = $mysql;
        }

        if ($dbName) {
            $mysql->select_db($dbName);
        }

        return $mysql;
    }

    /**
     * 连接Redis
     * @param null $serverName
     * @return redis
     */
    public function connectRedis($serverName = null)
    {
        $serverName = $serverName ? $serverName : 'default';

        if (!isset(DatabaseConfig::$redis_config[$serverName])) {
            $this->returnError("no_server_config");
        }

        $redisConfig = DatabaseConfig::$redis_config[$serverName];
        $redis = new redis();
        $ret = $redis->connect($redisConfig['host'], $redisConfig['port']);

        if (!$ret) {
            $this->returnError("error_redis_connect");
        }

        if (isset($redisConfig['password']) && $redisConfig['password']) {
            $redis->auth($redisConfig['password']);
        }

        if (isset($redisConfig['dbName'])) {
            $dbName = $redisConfig['dbName'];
        } else {
            $dbName = DatabaseConfig::Redis_Agent_DB;
        }

        if ($dbName) {
            $redis->select($dbName);
        }

        return $redis;
    }

    /**
     * 连接Mysql数据库
     * @param $serverName
     */
    public function connectMysql($serverName = null)
    {
        $serverName = $serverName ? $serverName : 'default';
        if (!isset(DatabaseConfig::$mysql_config[$serverName])) {
            $this->returnError("no_server_config");
        }

        $mysql = new MysqlDB(DatabaseConfig::$mysql_config[$serverName]);
        return $mysql;
    }
}